package com.ruoyi.web.controller.system;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.IAdminLuckyCommandService;
import com.ruoyi.system.service.IAdminPrizeRecordService;
import com.ruoyi.system.service.IAdminUserBetRecordService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import static com.ruoyi.quartz.task.LuckTask.getRegex;
import static com.ruoyi.quartz.task.LuckTask.getReverseNum;

/**
 * 预开奖统计Controller
 * 
 * @author ruoyi
 * @date 2021-11-12
 */
@RestController
@RequestMapping("/system/ballStatic")
public class AdminBallStaticController extends BaseController
{
    @Autowired
    private IAdminLuckyCommandService luckyCommandService;

    @Autowired
    private IAdminUserBetRecordService iAdminUserBetRecordService;


    @Autowired
    private IAdminPrizeRecordService iAdminPrizeRecordService;

    /**
     * 查询预开奖统计列表
     */
    @PreAuthorize("@ss.hasPermi('system:ballStatic:list')")
    @GetMapping("/list")
    public TableDataInfo list(AdminBallStatic adminBallStatic)
    {


        AdminPrizeRecord notPrizeRecord = iAdminPrizeRecordService.selectAdminPrizeRecordByPrizeTime();

        AdminUserBetRecord adminUserBetRecord = new AdminUserBetRecord();
        adminUserBetRecord.setPeriod(notPrizeRecord.getPeriod());
        List<AdminBallStatic> ballStatics = new ArrayList<>();
        List<AdminUserBetRecord> userBetRecords = iAdminUserBetRecordService.selectLuckUserBetRecordAndNotRoot(adminUserBetRecord);
        for (int i=1;i<=20;i++) {
            AdminBallStatic adminBallStatic1 = new AdminBallStatic();
            adminBallStatic1.setNum(Long.valueOf(i));
            int remain = i%4;
            String regx="";
            remain=remain==0?4:remain;

            int endBall = i;
            //是否双
            int single = i%2;

            //是否大
            int normal = i>10?1:0;
            String normalStr = normal == 0 ? "小" : "大";
            int startNum = 0;
            String command="";
            Double endNum = 0.00;

            int numOne=0;
            int numTwo=0;
            int numThree=0;
            Double price=0.00;
            for (AdminUserBetRecord singleStr:userBetRecords) {
                String luck =singleStr.getCommand();
                if (luck.contains("番") || luck.contains("翻") || luck.contains("花")
                        || luck.contains("番头")) {
                    if (luck.contains("番头")){
                        command="番头";
                        startNum= luck.indexOf("番头");
                    }else if(luck.contains("番")){
                        command="番";
                        startNum= luck.indexOf("番");
                    }else if(luck.contains("翻")){
                        command="翻";
                        startNum= luck.indexOf("翻");
                    }else if(luck.contains("花")){
                        command="花";
                        startNum= luck.indexOf("花");
                    }
                    String firstOne=luck.substring(0,startNum);
                    String firstTwo = firstOne.replaceAll(command, "");
                    String strOne=luck.substring(startNum,luck.length());
                    String strTwo = strOne.replaceAll(command, "");
                    endNum = Double.valueOf(strTwo);
                    numOne=Integer.valueOf(firstTwo);
                    AdminLuckyCommand luckyCommand = luckyCommandService.selectAdminLuckyCommandByCommand("番");
                    BigDecimal winPercent = luckyCommand.getWinPercent();
                    if (numOne==remain) {
                        price+=endNum-new Double(String.format("%.2f",winPercent.doubleValue()*endNum));
                    }else{
                        price+=endNum;
                    }

                    //1. 番、翻、花、番头


                }else if (luck.contains("角") || luck.contains("架桥") ) {
                    //2. 角、架桥
                    if (luck.contains("角")){
                        command="角";
                        startNum= luck.indexOf("角");
                    }else if(luck.contains("架桥")){
                        command="架桥";
                        startNum= luck.indexOf("架桥");
                    }
                    String first=luck.substring(0,1);
                    String firstOne=luck.substring(1,2);
                    String firstTwo = firstOne.replaceAll(command, "");
                    String strOne=luck.substring(startNum,luck.length());
                    String strTwo = strOne.replaceAll(command, "");
                    endNum = Double.valueOf(strTwo);
                    int numFirst=Integer.valueOf(first);
                    numOne=Integer.valueOf(firstTwo);
                    AdminLuckyCommand luckyCommand = luckyCommandService.selectAdminLuckyCommandByCommand("角");
                    BigDecimal winPercent = luckyCommand.getWinPercent();
                    if (numOne==remain || numFirst==remain) {
                        price+=endNum-new Double(String.format("%.2f",winPercent.doubleValue()*endNum));
                    }else{
                        price+=endNum;
                    }
                }else if (luck.contains("车") || luck.contains("推车") || luck.contains("挨车")
                        || luck.contains("三门")) {
                    //3. 车、推车、挨车、三门
                    if (luck.contains("推车")){
                        command="推车";
                        startNum= luck.indexOf("推车");
                    }else if(luck.contains("挨车")){
                        command="挨车";
                        startNum= luck.indexOf("挨车");
                    }else if(luck.contains("三门")){
                        command="三门";
                        startNum= luck.indexOf("三门");
                    }else if(luck.contains("车")){
                        command="车";
                        startNum= luck.indexOf("车");
                    }
                    String firstOne=luck.substring(0,startNum);
                    String firstTwo = firstOne.replaceAll(command, "");
                    String strOne=luck.substring(startNum,luck.length());
                    String strTwo = strOne.replaceAll(command, "");
                    endNum = Double.valueOf(strTwo);
                    numOne=Integer.valueOf(firstTwo);
                    AdminLuckyCommand luckyCommand = luckyCommandService.selectAdminLuckyCommandByCommand("车");
                    BigDecimal winPercent = luckyCommand.getWinPercent();
                    int reverse=getReverseNum(remain);
                    if (numOne!=reverse) {
                        price+=endNum-new Double(String.format("%.2f",winPercent.doubleValue()*endNum));
                    }else{
                        price+=endNum;
                    }

                }else if (luck.contains("念") || luck.contains("严") || luck.contains("粘")
                        || luck.contains("念头")) {
                    //4. 念、严、粘、念头
                    regx="^([1-4])((念头)|(念)|(严)|(粘))(?!\\1)([1-4])/((?:[1-9][0-9]\\d*))$";
                    boolean regexState = getRegex(regx, luck);
                    if (regexState) {
                        if (luck.contains("念头")) {
                            command = "念头";
                            startNum = luck.indexOf("念头");
                        } else if (luck.contains("严")) {
                            command = "严";
                            startNum = luck.indexOf("严");
                        } else if (luck.contains("粘")) {
                            command = "粘";
                            startNum = luck.indexOf("粘");
                        } else if (luck.contains("念")) {
                            command = "念";
                            startNum = luck.indexOf("念");
                        }
                        String first = luck.substring(0, 1);
                        String firstTwo = luck.replaceAll(command, "");
                        String firstOne = firstTwo.substring(1, 2);
                        String commandTwo = "/";
                        startNum = luck.indexOf(commandTwo);
                        String strOne = luck.substring(startNum, luck.length());
                        String strTwo = strOne.replaceAll(commandTwo, "");
                        endNum = Double.valueOf(strTwo);
                        int numFirst = Integer.valueOf(first);
                        numOne = Integer.valueOf(firstOne);

                        AdminLuckyCommand luckyCommand = luckyCommandService.selectAdminLuckyCommandByCommand("念头");
                        BigDecimal winPercent = luckyCommand.getWinPercent();
                        if (numFirst == remain) {
                            price+= endNum-new Double(String.format("%.2f", winPercent.doubleValue() * endNum));
                        } else if (numOne == remain) {
                            price += 0;
                        }else{
                            price+= endNum;
                        }
                    }else{
                        //5. 加念
                        regx="^([1-4])(念)(?!\\1)([1-4])(?!\\1|\\3)([1-4])/((?:[1-9][0-9]\\d*))$";
                        regexState = getRegex(regx, luck);
                        if (regexState) {
                            if (luck.contains("念")){
                                command="念";
                                startNum= luck.indexOf("念");
                            }
                            String first=luck.substring(0,1);
                            String firstTwo = luck.replaceAll(command, "");

                            String commandTwo="/";
                            startNum= luck.indexOf(commandTwo);
                            String strOne=luck.substring(startNum,luck.length());
                            String strTwo = strOne.replaceAll(commandTwo, "");
                            endNum = Double.valueOf(strTwo);
                            int numFirst=Integer.valueOf(first);
                            numOne=Integer.valueOf(firstTwo.substring(1,2));
                            numTwo=Integer.valueOf(firstTwo.substring(2,3));
                            AdminLuckyCommand luckyCommand = luckyCommandService.selectAdminLuckyCommandByCommand("加念");
                            BigDecimal winPercent = luckyCommand.getWinPercent();
                            if (numFirst==remain) {
                                price=endNum-new Double(String.format("%.2f",winPercent.doubleValue()*endNum));
                            }else if (numOne==remain || numTwo==remain){
                                price+=0;
                            }else{
                                price+= endNum;
                            }
                        }

                    }
                }else if (luck.contains("堂") || luck.contains("唐") || luck.contains("正")
                        || luck.contains("堂头")) {
                    //6. 堂、唐、正、堂头
                    if (luck.contains("堂头")){
                        command="堂头";
                        startNum= luck.indexOf("堂头");
                    }else if(luck.contains("堂")){
                        command="堂";
                        startNum= luck.indexOf("堂");
                    }else if(luck.contains("唐")){
                        command="唐";
                        startNum= luck.indexOf("唐");
                    }else if(luck.contains("正")){
                        command="正";
                        startNum= luck.indexOf("正");
                    }
                    String firstOne=luck.substring(0,startNum);
                    String firstTwo = firstOne.replaceAll(command, "");
                    String strOne=luck.substring(startNum,luck.length());
                    String strTwo = strOne.replaceAll(command, "");
                    endNum = Double.valueOf(strTwo);
                    numOne=Integer.valueOf(firstTwo);
                    AdminLuckyCommand luckyCommand = luckyCommandService.selectAdminLuckyCommandByCommand("堂头");
                    BigDecimal winPercent = luckyCommand.getWinPercent();
                    BigDecimal water = luckyCommand.getWater();

                    int reverse=0;

                    reverse=getReverseNum(remain);
                    if (numOne==remain) {
                        price+=endNum-new Double(String.format("%.2f",winPercent.doubleValue()*endNum));
                    }else if(numOne!=reverse){
                        price+=0;
                    }else{
                        price+= endNum;
                    }
                }else if (luck.contains("通")) {
                    //7. 通
                    if (luck.contains("通")){
                        command="通";
                        startNum= luck.indexOf("通");
                    }
                    String first=luck.substring(0,1);
                    String firstOne=luck.substring(startNum+1,startNum+3);
                    String commandTwo="/";
                    startNum= luck.indexOf(commandTwo);
                    String strOne=luck.substring(startNum,luck.length());
                    String strTwo = strOne.replaceAll(commandTwo, "");
                    endNum = Double.valueOf(strTwo);
                    int numFirst=Integer.valueOf(first);
                    numOne=Integer.valueOf(firstOne.substring(0,1));
                    numTwo=Integer.valueOf(firstOne.substring(1,2));
                    AdminLuckyCommand luckyCommand = luckyCommandService.selectAdminLuckyCommandByCommand("通");
                    BigDecimal winPercent = luckyCommand.getWinPercent();

                    if (numOne ==remain ||  numTwo ==remain) {
                        price+=endNum-new Double(String.format("%.2f",winPercent.doubleValue()*endNum));
                    }else if (numFirst!=remain){
                        price+=0;
                    }else{
                        price+= endNum;
                    }

                }else if (luck.contains("单") || luck.contains("双")) {
                    if (luck.contains("单")){
                        command="单";
                        startNum= luck.indexOf("单");
                    }else if(luck.contains("双")){
                        command="双";
                        startNum= luck.indexOf("双");
                    }

                    String strOne=luck.substring(startNum,luck.length());
                    String strTwo = strOne.replaceAll(command, "");
                    endNum = Double.valueOf(strTwo);
                    AdminLuckyCommand luckyCommand = luckyCommandService.selectAdminLuckyCommandByCommand("单");
                    BigDecimal winPercent = luckyCommand.getWinPercent();
                    if (command.equals(singleStr)) {
                        price+=endNum-new Double(String.format("%.2f",winPercent.doubleValue()*endNum));
                    }else{
                        price+= endNum;
                    }
                }else if (luck.contains("大") || luck.contains("小")) {
                    //9. 大、小
                    if (luck.contains("大")){
                        command="大";
                        startNum= luck.indexOf("大");
                    }else if(luck.contains("小")){
                        command="小";
                        startNum= luck.indexOf("小");
                    }

                    String strOne=luck.substring(startNum,luck.length());
                    String strTwo = strOne.replaceAll(command, "");
                    endNum = Double.valueOf(strTwo);
                    AdminLuckyCommand luckyCommand = luckyCommandService.selectAdminLuckyCommandByCommand("小");
                    BigDecimal winPercent = luckyCommand.getWinPercent();
                    if (command.equals(normalStr)) {
                        price+=endNum-new Double(String.format("%.2f",winPercent.doubleValue()*endNum));
                    }else{
                        price+= endNum;
                    }
                }else if (luck.contains("特")) {
                    //10. 特码
                    command="特";
                    startNum= luck.indexOf("特");
                    String strOne=luck.substring(startNum,luck.length());
                    String strTwo = strOne.replaceAll(command, "");

                    String strThree=luck.substring(0,startNum);
                    String[] strFour = strOne.replaceAll(command, "").split("\\/");
                    String[] split1 = strThree.split("/");
                    endNum = Double.valueOf(strTwo);
                    AdminLuckyCommand luckyCommand = luckyCommandService.selectAdminLuckyCommandByCommand("特");
                    BigDecimal winPercent = luckyCommand.getWinPercent();
                    boolean state=false;
                    for (String s:split1) {
                        Integer integer = Integer.valueOf(s);
                        if (integer==endBall) {
                            state=true;
                        }
                    }
                    if (state) {
                        price+=split1.length*endNum-new Double(String.format("%.2f",winPercent.doubleValue()*endNum));
                    }else{
                        price+= split1.length*endNum;
                    }
                }else{
                    regx="^([1-4])(?!\\1)([1-4])/((?:[1-9][0-9]{0,11}))$";
                    boolean regexState = getRegex(regx, luck);
                    BigDecimal winPercent =new BigDecimal(0);
                    command="/";
                    if (regexState) {
                        String first=luck.substring(0,1);
                        String firstOne=luck.substring(1,2);
                        startNum = luck.indexOf(command);
                        String strOne=luck.substring(startNum,luck.length());
                        String strTwo = strOne.replaceAll(command, "");
                        endNum = Double.valueOf(strTwo);
                        int numFirst=Integer.valueOf(first);
                        numOne=Integer.valueOf(firstOne);
                        AdminLuckyCommand luckyCommand = luckyCommandService.selectAdminLuckyCommandByCommand("角");
                        winPercent = luckyCommand.getWinPercent();
                        if (numOne==remain || numFirst==remain) {
                            price+=endNum-new Double(String.format("%.2f",winPercent.doubleValue()*endNum));
                        }else{
                            price+=endNum;
                        }
                    }else {
                        regx="^([1-4])(\\+)(?!\\1)([1-4])/((?:[1-9][0-9]\\d*))$";
                        regexState = getRegex(regx, luck);
                        if (regexState) {
                            command="\\+";
                            String first = luck.substring(0, 1);
                            String firstTwo = luck.replaceAll(command, "");
                            String firstOne = firstTwo.substring(1, 2);
                            String commandTwo = "/";
                            startNum = luck.indexOf(commandTwo);
                            String strOne = luck.substring(startNum, luck.length());
                            String strTwo = strOne.replaceAll(commandTwo, "");
                            endNum = Double.valueOf(strTwo);
                            int numFirst = Integer.valueOf(first);
                            numOne = Integer.valueOf(firstOne);

                            AdminLuckyCommand luckyCommand = luckyCommandService.selectAdminLuckyCommandByCommand("念头");
                            winPercent = luckyCommand.getWinPercent();
                            if (numFirst == remain) {
                                price+=endNum- new Double(String.format("%.2f", winPercent.doubleValue() * endNum));
                            } else if (numOne == remain) {
                                price+=0;
                            }else{
                                price+=endNum;
                            }

                        }else {
                            command="/";
                            regx="^([1-4])/((?:[1-9][0-9]\\d*))$";
                            regexState = getRegex(regx, luck);
                            if (regexState) {
                                String firstOne=luck.substring(0,1);
                                startNum= luck.indexOf(command);
                                String strOne=luck.substring(startNum,luck.length());
                                String strTwo = strOne.replaceAll(command, "");
                                endNum = Double.valueOf(strTwo);
                                numOne=Integer.valueOf(firstOne);
                                AdminLuckyCommand luckyCommand = luckyCommandService.selectAdminLuckyCommandByCommand("番");
                                winPercent = luckyCommand.getWinPercent();
                                if (numOne==remain) {
                                    price+=endNum-  new Double(String.format("%.2f",winPercent.doubleValue()*endNum));
                                }else{
                                    price+=endNum;
                                }
                            }else {
                                command="\\+";
                                regx="^([1-4])(\\+)(?!\\1)([1-4])(?!\\1|\\3)([1-4])/((?:[1-9][0-9]\\d*))$";
                                regexState = getRegex(regx, luck);
                                if (regexState) {
                                    String first=luck.substring(0,1);
                                    String firstTwo = luck.replaceAll(command, "");

                                    String commandTwo="/";
                                    startNum= luck.indexOf(commandTwo);
                                    String strOne=luck.substring(startNum,luck.length());
                                    String strTwo = strOne.replaceAll(commandTwo, "");
                                    endNum = Double.valueOf(strTwo);
                                    int numFirst=Integer.valueOf(first);
                                    numOne=Integer.valueOf(firstTwo.substring(1,2));
                                    numTwo=Integer.valueOf(firstTwo.substring(2,3));
                                    AdminLuckyCommand luckyCommand = luckyCommandService.selectAdminLuckyCommandByCommand("加念");
                                    winPercent = luckyCommand.getWinPercent();
                                    if (numFirst==remain) {
                                        price+=endNum-new Double(String.format("%.2f",winPercent.doubleValue()*endNum));
                                    }else if (numOne==remain || numTwo==remain){

                                    }else{
                                        price+=endNum;
                                    }
                                }else{
                                    regx="^([1-4])(?!\\1)([1-4])(-)(?!\\1|\\2)([1-4])/((?:[1-9][0-9]\\d*))$";
                                    regexState = getRegex(regx, luck);
                                    if (regexState) {
                                        String first=luck.substring(0,1);
                                        String commandTwo="/";
                                        startNum= luck.indexOf(commandTwo);
                                        String strOne=luck.substring(startNum,luck.length());
                                        String strTwo = strOne.replaceAll(commandTwo, "");
                                        endNum = Double.valueOf(strTwo);
                                        int numFirst=Integer.valueOf(first);
                                        numOne=Integer.valueOf(luck.substring(1,2));
                                        numTwo=Integer.valueOf(luck.substring(3,4));
                                        AdminLuckyCommand luckyCommand = luckyCommandService.selectAdminLuckyCommandByCommand("通");
                                        winPercent = luckyCommand.getWinPercent();

                                        if (numFirst ==remain ||  numOne ==remain) {
                                            price+=endNum-new Double(String.format("%.2f",winPercent.doubleValue()*endNum));
                                        }else if (numTwo!=remain){

                                        }else{
                                            price+=endNum;
                                        }
                                    }else{
                                        regx="^([1-4])(-)(?!\\1)([1-4])/((?:[1-9][0-9]\\d*))$";
                                        regexState = getRegex(regx, luck);
                                        if (regexState) {
                                            String firstOne=luck.substring(0,1);
                                            String firstTwo=luck.substring(2,3);
                                            command="/";
                                            startNum= luck.indexOf(command);
                                            String strOne=luck.substring(startNum,luck.length());
                                            String strTwo = strOne.replaceAll(command, "");
                                            endNum = Double.valueOf(strTwo);
                                            numOne=Integer.valueOf(firstOne);
                                            numTwo=Integer.valueOf(firstTwo);
                                            AdminLuckyCommand luckyCommand = luckyCommandService.selectAdminLuckyCommandByCommand("堂头");
                                            winPercent = luckyCommand.getWinPercent();
                                            BigDecimal water = luckyCommand.getWater();

                                            int reverse=0;

                                            reverse=getReverseNum(remain);
                                            if (numOne==remain) {
                                                price+=endNum-new Double(String.format("%.2f",winPercent.doubleValue()*endNum));
                                            }else if(numTwo!=remain){

                                            }else{
                                                price+=endNum;
                                            }
                                        }
                                    }
                                }

                            }
                        }
                    }

                }

            }
            adminBallStatic1.setPrice(new BigDecimal(price));
            ballStatics.add(adminBallStatic1);
        }
        return getDataTable(ballStatics);
    }


}
